package com.binvshe.binvshe.mine.corporation.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.app.MainActivity;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.mine.corporation.ClubInfoActivity;
import com.binvshe.binvshe.mine.view.CommentDialog;
import com.binvshe.binvshe.organization.ClubMemberActivity;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.frameworkbase.customviews.iconcontent.IconTextArrowButton;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubSettingFragment extends BaseFragment {

    @Bind(R.id.mItabtnDissolve)
    IconTextArrowButton mItabtnDissolve;
    @Bind(R.id.mTtbtnAttorn)
    IconTextArrowButton mTtbtnAttorn;
    @Bind(R.id.mTtbtnSetInfo)
    IconTextArrowButton mTtbtnSetInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        showToolbar();
        setTitle("社团管理");
        getCustomToolbar().enableBackButton();
        return view;
    }

    public static ClubSettingFragment newInstance() {
        Bundle args = new Bundle();
        ClubSettingFragment fragment = new ClubSettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public ClubSettingFragment() {
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_club_setting, null);
    }

    @OnClick({R.id.mItabtnDissolve,
            R.id.mTtbtnAttorn,
            R.id.mTtbtnSetInfo,
            R.id.mTtbtnFriends,
            R.id.mTtbtnMember,
            R.id.mTtbtnNotice})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mItabtnDissolve:
                //解散社团
                showDelClubDialog();
                break;
            case R.id.mTtbtnSetInfo:
                //修改资料
                ClubInfoActivity.start(getContext());
                break;

            case R.id.mTtbtnFriends:
                //邀请好友
                FragmentUtils.addFragment(getFragmentManager(), R.id.mClubContainer, FriendsFragment.newInstance(AccountManager.getCurrentAccount().getClub().getId()));
                break;
            case R.id.mTtbtnAttorn:
                //转出社团
                FragmentUtils.addFragment(getFragmentManager(), R.id.mClubContainer, TansferFragment.newInstance());
                break;
            case R.id.mTtbtnMember:
                //成员管理
                ClubMemberActivity.start(getContext(), AccountManager.getCurrentAccount().getClub().getId(), Integer.parseInt(AccountManager.getCurrentAccount().getUserId()));
                break;
            case R.id.mTtbtnNotice:
                CommentDialog commentDialog = CommentDialog.newInstance("公告管理", "公告内容", 5, false);
                commentDialog.show(getFragmentManager(), "");
                commentDialog.setOnClickable(
                        new CommentDialog.CommentClickable() {
                            @Override
                            public void btnEnter(String comment) {
                                IHttpRequest request = new MyHttpRequest(getContext());
                                JSONObject jobj = new JSONObject();
                                try {
                                    jobj.put(ParamKey.Club.ID, AccountManager.getCurrentAccount().getClub().getId());
                                    jobj.put("note",comment);
                                    request.put(UrlCenter.CLUB_UPDATE, jobj, new DataRequestListener<Object>(Object.class) {
                                        @Override
                                        public void onSuccess(Object reg) {
                                            Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                        }

                                        @Override
                                        public void onStart(Context context, String url) {
                                            enableProgress(false);
                                            super.onStart(context, url);
                                        }

                                        @Override
                                        public void onFailure(Throwable t, int errorNo, String strMsg) {
                                            EasyToast.showToast(getContext(), strMsg);
                                        }
                                    });
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                );
                break;
        }
    }

    private void showDelClubDialog() {
        final AlertDialogFragment alertDialogFrag = new AlertDialogFragment();
        alertDialogFrag.setMessage("是否解散该社团?")
                .setPositiveButton("解散", false, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyHttpRequest request = new MyHttpRequest(getActivity());
                        String url = String.format(UrlCenter.DEl_CLUB, AccountManager.getCurrentAccount().getClub().getId());
                        request.delete(url, null, new DataRequestListener<String>(String.class) {
                            @Override
                            public void onStart(Context context, String url) {
                                enableProgress(false);
                                super.onStart(context, url);
                            }

                            @Override
                            public void onSuccess(String str) {
                                super.onSuccess(str);
                                EasyToast.showToast(getContext(), "解散成功!");
                                AccountManager.getCurrentAccount().setInClub(false);
                                AccountManager.getCurrentAccount().setClub(null);
                                startActivity(new Intent(getActivity(), MainActivity.class));
                                alertDialogFrag.dismiss();
                            }

                            @Override
                            public void onFailure(Throwable t, int errorNo, String strMsg) {
                                super.onFailure(t, errorNo, strMsg);
                                EasyToast.showToast(getContext(), strMsg);
                            }
                        });
                    }
                })
                .setNegativeButton("取消", true, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialogFrag.dismiss();
                    }
                })
                .show(getFragmentManager());
    }
}
